﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Text;
using WinnerCMS.Common;
using WinnerCMS.Model;
namespace WinnerCMS.Data
{
    /// <summary>
    /// Vote 的摘要说明。
    /// </summary>
    public class VoteDal
    {
        /// <summary>
        /// 添加投票主题
        /// </summary>
        /// <param name="model"></param>
        public int Add(Vote model)
        {
            using (Entities db = new Entities())
            {
                db.Votes.Add(model);
                db.SaveChangesAsync().Wait();

                return model.Id;
            }
        }
        /// <summary>
        /// 修改投票主题
        /// </summary>
        /// <param name="model"></param>
        public void Edit(Vote model)
        {
            using (Entities db = new Entities())
            {
                db.Votes.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();

            }

        }
        /// <summary>
        /// 删除投票主题
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            using (Entities db = new Entities())
            {
                var q = db.Votes.FirstOrDefault(x => x.Id == id);
                db.Votes.Remove(q);
                db.SaveChangesAsync().Wait();
            }
        }

        /// <summary>
        /// 加载投票主题
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Vote GetInfo(int id)
        {
            using (Entities db = new Entities())
            {
                return db.Votes.FirstOrDefault(x => x.Id == id);
            }
        }

        /// <summary>
        /// 审核投票主题
        /// </summary>
        /// <param name="id"></param>
        public void Pass(int id)
        {
            using (Entities db = new Entities())
            {
                var q = db.Votes.FirstOrDefault(x => x.Id == id);
                if (q != null)
                {
                    q.Pass = !q.Pass;
                    db.SaveChangesAsync().Wait();
                }
            }
        }

        /// <summary>
        /// 取得投票,給前台用的
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Vote Load(int id)
        {
            using (Entities db = new Entities())
            {
                return db.Votes.FirstOrDefault(x => x.Id == id && x.Pass);
            }
        }
        /// <summary>
        /// 前台显示投票
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Vote ShowVote(int id)
        {
            using (Entities db = new Entities())
            {
                db.Configuration.ProxyCreationEnabled = false;
                db.Configuration.LazyLoadingEnabled = false;
                var q = from x in db.Votes.Include(x => x.VoteChoices)
                        where x.Id == id && x.Pass
                        select x;
                return q.FirstOrDefault();
            }
        }

        /// <summary>
        /// 绑定
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Vote> BindList(PagingParam pp)
        {
            using (Entities db = new Entities())
            {
                var query = (from u in db.Votes
                             join y in
                                 (from choice in db.VoteChoices group choice by choice.VoteId into g select new { VoteID = g.Key, CountChoice = g.Count() })
                             on u.Id equals y.VoteID
                             join z in
                                 (from choice in db.VoteChoices group choice by choice.VoteId into g select new { VoteID = g.Key, SUMVoteNum = g.Sum(x => x.VoteNum) })
                             on u.Id equals z.VoteID

                             select new { Id = u.Id, Title = u.Title, CheckInTime = u.CheckInTime, VoteType = u.VoteType, SUMVoteNum = z.SUMVoteNum, CountChoice = y.CountChoice }

                             );

                pp.RecordCount = query.Count();

                return query.OrderByDescending(x => x.Id).Skip(pp.Skip).Take(pp.PageSize).ToList().Select(u => new Vote()
                {
                    Id = u.Id,
                    Title = u.Title,
                    CheckInTime = u.CheckInTime,
                    VoteType = u.VoteType,
                    SUMVoteNum = u.SUMVoteNum,
                    CountChoice = u.CountChoice
                }).ToList();

            }
        }
    }
}